/**
 * 
 */
package com.zxc.mongodb;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.zxc.jdbc.JdbcDemo;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * @author Administrator
 *
 */
public class QiWenDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 获取链接    
        MongoClient mongoClient = new MongoClient("192.168.100.10", 27017);   
        
        // 获取数据库    
        MongoDatabase database = mongoClient.getDatabase("base_info");    
        
//        fetchCityInfo(database);
        
        mergeCityData(database);
        
        mongoClient.close();
	}

	public static void mergeCityData(MongoDatabase database) {
        MongoCollection<org.bson.Document> qiwenCol = database.getCollection("qiwen"); 
        
        MongoCollection<org.bson.Document> tempCityCol = database.getCollection("t_temp_city"); 
        
        MongoCollection<org.bson.Document> qiwenCityCol = database.getCollection("qiwen_city"); 
        
        List<JSONObject> qiwenlist = new ArrayList<JSONObject>();
        qiwenCol.find().forEach(new Block<org.bson.Document>() {
        	
			@Override
			public void apply(org.bson.Document t) {
				JSONObject o = new JSONObject();
				o.putAll(t);
				qiwenlist.add(o);
			}
		});
        
        List<JSONObject> tempcitylist = new ArrayList<JSONObject>();
        tempCityCol.find().forEach(new Block<org.bson.Document>() {
        	
			@Override
			public void apply(org.bson.Document t) {
				JSONObject o = new JSONObject();
				o.putAll(t);
				tempcitylist.add(o);
			}
		});
        
        qiwenlist.forEach(qiwen->{
        	JSONObject city = tempcitylist.stream().filter(tempcity->{
        		return StrUtil.equals(qiwen.getStr("cityName"), tempcity.getStr("NAME_"));
        	}).findFirst().orElse(new JSONObject());
        	
        	if(MapUtil.isNotEmpty(city)) {
        		org.bson.Document doc = new org.bson.Document();
    			doc.put("cityname", city.getStr("NAME_"));
    			doc.put("citycode", qiwen.getStr("cityCode"));
    			doc.put("cityextid", city.getStr("EXT_ID_"));
    			qiwenCityCol.insertOne(doc);
        	}
        });
	}
	
	public static void fetchCityInfo(MongoDatabase database) {
		// 进入某个文档集    
        MongoCollection<org.bson.Document> collection = database.getCollection("qiwen"); 

        JSONArray list = new JSONArray();
        
        String result = HttpUtil.get("http://www.nmc.cn/rest/province/all?_=1727139929533");
        JSONArray provinceArr = JSONUtil.parseArray(result);
        for (Object object : provinceArr) {
			JSONObject province = (JSONObject) object;
			String code = province.getStr("code");
			result = HttpUtil.get("http://www.nmc.cn/rest/province/"+code+"?_=1727139929536");
	        System.out.println(result);
	        JSONArray cityArr = JSONUtil.parseArray(result);
	        for (Object object2 : cityArr) {
	        	JSONObject city = (JSONObject) object2;
	        	
	        	JSONObject o = new JSONObject();
	        	o.putOpt("provinceCode", code);
	        	o.putOpt("provinceName", city.get("province"));
	        	o.putOpt("cityCode", city.get("code"));
	        	o.putOpt("cityName", city.get("city"));
	        	list.add(o);
			}
		}
        
        list.forEach(o->{
        	org.bson.Document doc = new org.bson.Document();
			doc.putAll((JSONObject)o);
    		collection.insertOne(doc);
        });
	}
}
